<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>sessionStorage/localStorage</title>
</head>
<body>
  <p id="p1"></p>
  <p id="p2"></p>
  <p id="p3"></p>
  <p id="p4"></p>
  <p>
    <button onclick="addsessionStorage()">添加sessionStorage</button>
    <button onclick="addlocalStorage()">添加localStorage</button>
  </p>
  <p>
    <button onclick="remove_name1()">删除sessionStorage name1</button>
    <button onclick="remove_age1()">删除sessionStorage age2</button>
  </p>
  <p>
    <button onclick="remove_name2()">删除localStorage name2</button>
    <button onclick="remove_age2()">删除localStorage age2</button>
  </p>
  <p>
    <button onclick="remove_all1()">删除sessionStorage</button>
    <button onclick="remove_all2()">删除localStorage</button>
  </p>
  <script>
    var p1 = document.getElementById('p1');
    var p2 = document.getElementById('p2');
    var p3 = document.getElementById('p3');
    var p4 = document.getElementById('p4');
    function showText() {
      // 获取数据个数
      var size = sessionStorage.length;
      console.log("sessionStorage length:", size);
      p1.innerHTML = "sessionStorage length:" + size;
      size = localStorage.length;
      p2.innerHTML = "localStorage length:" + size;
      // 获取数据内容
      var name1 = sessionStorage.getItem("name1");
      var age1 = sessionStorage.getItem("age1");
      p3.innerHTML = "sessionStorage name1:" + name1 + ",age1:" + age1;
      
      var name2 = localStorage.getItem("name2");
      var age2 = localStorage.getItem("age2");
      p4.innerHTML = "localStorage name2:" + name2 + ",age2:" + age2;
    }
    showText();

    function addsessionStorage() {
      sessionStorage.setItem("name1","lily");
      sessionStorage.setItem("age1","17");
      showText()
    }
    function addlocalStorage() {
      localStorage.setItem("name2", "lucy");
      localStorage.setItem("age2", "18");
      showText()
    }

    function remove_name1() {
      sessionStorage.removeItem("name1");
      showText()
    }
    function remove_age1() {
      sessionStorage.removeItem("age1");
      showText()
    }
    function remove_name2() {
      localStorage.removeItem("name2");
      showText()
    }
    function remove_age2() {
      localStorage.removeItem("age2");
      showText()
    }
    function remove_all1() {
      sessionStorage.clear();
      showText()
    }
    function remove_all2() {
      localStorage.clear();
      showText()
    }
  </script>
</body>
</html>